package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.persistence.Page;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.HandoverException;
import com.chain.qs.modules.biz.service.HandoverExceptionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 值班设置Controller
 *
 * @author Wang zhiping
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/handoverException")
public class HandoverExceptionController extends BaseController {

    @Resource
    private HandoverExceptionService handoverExceptionService;

    @ModelAttribute
    public HandoverException get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return handoverExceptionService.get(id);
        } else {
            return new HandoverException();
        }
    }

    //    @RequiresPermissions("sys:handover:view")
    @RequestMapping(value = {"list", ""})
    public String list( HandoverException handoverException,  HttpServletRequest request, HttpServletResponse response,Model model ) {
        Page<HandoverException> page = handoverExceptionService.findPage(new Page<HandoverException>(request, response), handoverException);
        model.addAttribute("page", page);
        return "modules/biz/handoverExceptionList";
    }

    //    @RequiresPermissions("sys:handover:view")
    @RequestMapping(value = "form")
    public String from(HandoverException handoverException, Model model) {
        model.addAttribute(handoverException);
        return "modules/biz/handoverExceptionForm";
    }

    //    @ResponseBody
    @RequiresPermissions("sys:handover:edit")
    @RequestMapping(value = "save")
    public String save(HandoverException handoverException) {
        handoverExceptionService.save(handoverException);
        return "redirect:" + adminPath + "/biz/handoverException/";
    }

    @RequiresPermissions("sys:handover:edit")
    @RequestMapping(value = "delete")
    public String delete(HandoverException handoverException, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/handoverException/";
        }
        handoverExceptionService.delete(handoverException);
        addMessage(redirectAttributes, "删除排班成功");
        return "redirect:" + adminPath + "/biz/handoverException/";
    }


}
